package com.taofuxn.admin.api.controller.sys;


import com.taofuxn.admin.api.controller.common.BaseController;
import com.taofuxn.admin.api.model.RoleModel;
import com.taofuxn.admin.api.service.SysUserConfigService;
import com.taofuxn.admin.common.model.BaseResults;
import com.taofuxn.admin.common.model.PageRequest;
import com.taofuxn.admin.common.model.PageResponse;
import com.taofuxn.admin.common.util.CheckUtil;
import com.taofuxn.admin.dao.entity.TbSysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("角色管理")
@RestController
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    protected SysUserConfigService resourceService;


    @ApiOperation(value = "角色分页列表", notes = "参数，page:页号；limit：页大小；其它查询条件字段")
    @GetMapping("page")
    public BaseResults<PageResponse<RoleModel>> page(PageRequest<TbSysRole> pageParam) {
        if (pageParam.getParam() == null) {
            pageParam.setParam(new TbSysRole());
        }
        PageResponse<RoleModel> rolePage = resourceService.findRolePage(pageParam);
        return BaseResults.success(rolePage);
    }

    @PostMapping("add")
    public BaseResults add(@RequestBody RoleModel role) {
        if (StringUtils.isBlank(role.getName())) {
            return BaseResults.fail("角色名称不能为空");
        }
        resourceService.saveOrUpdateRole(role);
        return BaseResults.success(null);
    }

    @PostMapping("edit")
    public BaseResults edit(@RequestBody RoleModel role) {
        if (!CheckUtil.checkIdForLong(role.getId())) {
            return BaseResults.fail("角色ID不能为空");
        }
        if (StringUtils.isBlank(role.getName())) {
            return BaseResults.fail("角色名称不能为空");
        }
        resourceService.saveOrUpdateRole(role);
        return BaseResults.success(null);
    }
//
//    @ApiOperation(value = "获取角色信息", notes = "返回kv为：role：角色对象;resourceIds:角色关联的菜单资源权限")
//    @ResponseBody
//    @GetMapping("get/{id}")
//    public BaseResults<Map<String, Object>> get(@PathVariable Long id) {
//        RoleModel role = null;
//        if (id != null && id > 0)
//            role = resourceService.getRoleById(id);
//        if (role == null)
//            return BaseResults.fail("角色不存在");
//
//        String resourceIds = buildResourceIds(role);
//        Map<String, Object> map = new HashMap<>();
//        map.put("role", role);
//        map.put("resourceIds", resourceIds);
//        return BaseResults.succ(map);
//    }
//


    @ApiOperation("角色删除")
    @DeleteMapping("delete/{id}")
    public BaseResults delete(@PathVariable("id") Long id) {
        if (!CheckUtil.checkIdForLong(id)) {
            return BaseResults.fail("角色ID不能为空");
        }
        resourceService.deleteRole(id);
        return BaseResults.success(null);
    }

    @PostMapping("batchdelete")
    @ResponseBody
    public BaseResults batchDelete(@RequestBody List<RoleModel> modelList) {
        if (modelList == null || modelList.size() == 0) {
            return BaseResults.fail("请选择需要删除的记录");
        }
        for (RoleModel model : modelList) {
            resourceService.deleteRole(model.getId());
        }
        return BaseResults.success(null);
    }

}
